package com.jnu.myfirstapplication.data;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

public class DataSaver {
    public void save(Context context, ArrayList<Book> data,ArrayList<String> bookShelfString) {
        try{
            FileOutputStream dataStream=context.openFileOutput("mydata.dat",Context.MODE_PRIVATE);
            ObjectOutputStream out=new ObjectOutputStream(dataStream);
            out.writeObject(data);
            out.writeObject(bookShelfString);
            out.close();
            dataStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //@NonNull
    public ArrayList<Book> LoadBookList(Context context){
        ArrayList<Book> data=new ArrayList<>();
        try{
            FileInputStream fileIn=context.openFileInput("mydata.dat");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            data=(ArrayList<Book>)in.readObject();
            in.close();
            fileIn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }

    public ArrayList<String> LoadBookShelfName(Context context){
        ArrayList<String> data=new ArrayList<>();
        try{
            FileInputStream fileIn=context.openFileInput("mydata.dat");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            ArrayList<Book> useless=(ArrayList<Book>)in.readObject();
            data=(ArrayList<String>)in.readObject();
            in.close();
            fileIn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }

    public void saveHashMap(Context context, HashMap<String,ArrayList<Book>> hashMap) {
        try{
            FileOutputStream dataStream=context.openFileOutput("mydata2.dat",Context.MODE_PRIVATE);
            ObjectOutputStream out=new ObjectOutputStream(dataStream);
            out.writeObject(hashMap);
            out.close();
            dataStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public HashMap<String,ArrayList<Book>> LoadHashMap(Context context){
        HashMap<String,ArrayList<Book>> data=new HashMap<>();
        try{
            FileInputStream fileIn=context.openFileInput("mydata2.dat");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            data=(HashMap<String,ArrayList<Book>>)in.readObject();
            in.close();
            fileIn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }
    public void saveLabel(Context context,ArrayList<String> arrayList){
        try{
            FileOutputStream dataStream=context.openFileOutput("mydata3.dat",Context.MODE_PRIVATE);
            ObjectOutputStream out=new ObjectOutputStream(dataStream);
            out.writeObject(arrayList);
            out.close();
            dataStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public ArrayList<String> LoadLabel(Context context){
        ArrayList<String> data=new ArrayList<>();
        try{
            FileInputStream fileIn=context.openFileInput("mydata3.dat");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            data=(ArrayList<String>)in.readObject();
            in.close();
            fileIn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }


    public void saveHashMaps(Context context, HashMap<String,HashMap<String, ArrayList<Book>>> hashMaps) {
        try{
            FileOutputStream dataStream=context.openFileOutput("mydata4.dat",Context.MODE_PRIVATE);
            ObjectOutputStream out=new ObjectOutputStream(dataStream);
            out.writeObject(hashMaps);
            out.close();
            dataStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public HashMap<String,HashMap<String, ArrayList<Book>>> LoadHashMaps(Context context){
        HashMap<String,HashMap<String, ArrayList<Book>>> data=new HashMap<>();
        try{
            FileInputStream fileIn=context.openFileInput("mydata4.dat");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            data=(HashMap<String,HashMap<String, ArrayList<Book>>>)in.readObject();
            in.close();
            fileIn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }
    public void saveEachBook(Context context,HashMap<String,HashMap<String,int[]>> eachBookHash) {
        try{
            FileOutputStream dataStream=context.openFileOutput("eachBook.dat",Context.MODE_PRIVATE);
            ObjectOutputStream out=new ObjectOutputStream(dataStream);
            out.writeObject(eachBookHash);
            out.close();
            dataStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public HashMap<String,HashMap<String,int[]>> LoadEachBook(Context context){
        HashMap<String,HashMap<String,int[]>> data=new HashMap<>();
        try{
            FileInputStream fileIn=context.openFileInput("eachBook.dat");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            data=(HashMap<String,HashMap<String,int[]>>)in.readObject();
            in.close();
            fileIn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }

    public void saveBookCoverNumber(Context context,int number) {
        try{
            FileOutputStream dataStream=context.openFileOutput("bookCover.dat",Context.MODE_PRIVATE);
            ObjectOutputStream out=new ObjectOutputStream(dataStream);
            out.writeObject(number);
            out.close();
            dataStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public int loadBookCoverNumber(Context context){
        int data=10;
        try{
            FileInputStream fileIn=context.openFileInput("bookCover.dat");
            ObjectInputStream in =new ObjectInputStream(fileIn);
            data=(int)in.readObject();
            in.close();
            fileIn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }


}
